import { LevelData } from "./js/game/levelmanager";
import Utils from "./js/game/utils";
import EditCell from "./js/edit/EditCell";
import GameConst from "./js/game/GameConst";
import EditGlobal from "./js/edit/EditGlobal";
import EditTool from "./js/edit/EditTool";

export default class EditUI extends Laya.Scene {
    constructor() {
        super();
        this.cells = [];     
         
    }

    onAwake() {
        
        EditGlobal.main = this; 
        var  i =0;
        for (const key in EditGlobal.config) {
            var img = new EditTool(key);
            img.x = (i%9)*80;
            img.y= Math.floor(i/9)*80;
            this.tools.addChild(img);    
            i++;
        } 
        this.btn_save.on(Laya.Event.CLICK,this,this.save);
        this.loadLevel();
    }

    save(){
        var cfg = '';
        for(var i=0;i<GameConst.LEVEL_W;i++){
            for(var j=0;j<GameConst.LEVEL_H;j++){
                cfg += this.cells[i][j].getData();
            }
        }
        cfg+= "-"+this.target.selectedIndex;
        cfg+= "-"+this.custom1.text;
        cfg+= "-"+this.custom2.text ;
        cfg+= "-"+this.star1.text;
        cfg+= "-"+this.star2.text;
        cfg+= "-"+this.star3.text;
        cfg+= "-"+this.times.text;
        cfg+= "-"+this.luobo.text;
        var tel = this.teleport.text;
        var jelly = this.jelly.text;
        if(tel||jelly){
            cfg+= "-"
            if(tel){
                cfg+= tel;
            }
            if(jelly){
                cfg+= '-'+jelly;
            }
        }
        var req = new Laya.HttpRequest(); 
        req.once(Laya.Event.COMPLETE,this,this.onSaved);
        req.send("lvl.php",{'lvl':this.lvl,'type':'save',data:cfg},'post','text');
    }

    onSaved(data){
        alert('保存成功');
        window.location.reload();
    }
    loadLevel() {
        this.lvl = Utils.getQueryString('lvl',0);
        var req = new Laya.HttpRequest(); 
        req.once(Laya.Event.COMPLETE,this,this.onLevelLoad);
        req.send("lvl.php",{'lvl':this.lvl,'type':'get'},'post','text');
        //var cfg = "#00202000000000000000100000100000000000202000001000000000100000202000000000001000000000000000000000100000000000202000000000000000100000100000000000001000001000001000000000202000000000100000000000000000-4-200-1-200-300-400-10-1111111111--1111111111";

    }

    onLevelLoad(cfg){
        var lvdata = new LevelData(1, "#"+cfg, 0);
        this.levelData = lvdata;

        this.star1.text = lvdata.starValues[0];
        this.star2.text = lvdata.starValues[1];
        this.star3.text = lvdata.starValues[2];
        this.target.selectedIndex = lvdata.targetData;
        this.times.text = lvdata.movesLeft;
        this.custom1.text = lvdata.customData;
        this.custom2.text = lvdata.customData2;
        this.luobo.text = lvdata.pushPositionStr;
        this.teleport.text = lvdata.teleportStr;
        this.jelly.text = lvdata.jellyPushStr;


        this.cellData = lvdata.cellData;
        for (var i = 0; i < GameConst.LEVEL_W; ++i) {
            for (var c = 0; c < GameConst.LEVEL_H; ++c) {
                var h = this.getCellDataType(this.cellData, i, c);
                if (h == "2")
                    this.hasWhiteChoco = true;
                if (h == "3")
                    this.hasBlackChoco = true;
                if (h == "7")
                    this.hasBomb = true;
            }
        }
        this.generateCells();
    }

    getObjectType(col, row) {
        if (!this.isGenerated) {
            var n = this.getCellDataColor(col, row);
            return n - 1
        }
        return this.target.getExactType(col, row)
    };
    getCellDataType(col, row) {
        return this.cellData.charAt(2 * (row + col * GameConst.LEVEL_H) + 1);
    };
    getCellDataColor(col, row) {
        return this.cellData.charAt(2 * (row + col * GameConst.LEVEL_H) + 2);
    };
    generateCells() {
        for (var row = 0; row < GameConst.LEVEL_W; row++) {
            var cellArr = [];
            for (var col = 0; col < GameConst.LEVEL_H; col++) {
                var cell = new EditCell(row,col);
                this.game_gem.addChild(cell);
                var type = this.getCellDataType(row, col);
                var color = this.getCellDataColor(row, col);
                cell.setData(type,color);
                cellArr.push(cell);

                continue;
                switch (type) {
                    case "0":
                    case "":
                        break;
                    case "1":
                        cell.setBlock(0);
                        break;
                    case "2":
                        cell.setBlock(1);
                        break;
                    case "3":
                        cell.setBlock(2);
                        break;
                    case "4":
                        cell.setBlock(4);
                        break;
                    case "5":
                        cell.setMark(1);
                        break;
                    case "6":
                        cell.setMark(2);
                        break;
                    case "7":
                        cell.setBlock(5);
                        break;
                    case "8":
                        cell.setBlock(6);
                        break;
                    case "9":
                        cell.setMark(3);
                        break;
                    case "a":
                        cell.setBlock(3);
                        break;
                    case "b":
                        cell.setBlock(7);
                        break
                }
               
                if (color == 5) {
                    cell.setBlock(4);
                }
                else if (color == 6) {
                    cell.setBlock(7)
                }else{
                    //cell.setColor(color);
                }
            }
            this.cells.push(cellArr);
        }
        if (this.levelData.jellyPushPositions) {
            for (var col = 0; col < this.fieldWidth; ++col)
                this.cells[col][0].isPushEnabled = this.levelData.jellyPushPositions[col]
        }
        var o = [];
        var u = 18;
        for (var col = 0; col < u; ++col)
            o.push([null, null]);
        var tplLen = this.levelData.teleportData.length;
        for (var col = 0; col < tplLen; ++col) {
            var f = this.levelData.teleportData[col];
            o[f[3]][f[2]] = this.cells[f[0]][f[1]]
        }
        for (col = 0; col < u; ++col) {
            if (o[col][0] && o[col][1]) {
                o[col][0].setTeleport(true, o[col][1]);
                o[col][1].setTeleport(false, o[col][0])
            }
        }
    }





}